//
//  SwiftFunc.swift
//  SwiftLearning
//
//  Created by 黄龙 on 2023/12/13.
//

import UIKit

//375*667(iphone8) 414*896(ihponex)
let screen_W = UIScreen.main.bounds.width
let screen_H = UIScreen.main.bounds.height
//以390*844(iphone12\13等 w/h=0.462)为基数的宽高比例
let _SCALE_HEIGHT_UNIT =  screen_H/844.0
let _SCALE_WIDTH_UNIT  =   screen_W/390.0

typealias os_block_int = (_ index:Int) -> Void
typealias os_block_string = (_ title:String?) -> Void

class SwiftFunc: NSObject {
//MARK: - 单例创建方法及注意事项
//单例初始化方法1：
        static let sharedInstance = SwiftFunc() //单例
        
//单例初始化方法2(此种写法，可以调用destory将单例销毁)：
    //    private static var _sharedInstance:HLUniversalFunc?
    //    class func getInstance()->HLUniversalFunc{
    //        guard let instance = _sharedInstance else{
    //            _sharedInstance = HLUniversalFunc()
    //            return _sharedInstance!
    //        }
    //        return instance
    //    }
    ////销毁单例
    //    class func destory(){
    //        _sharedInstance = nil
    //    }
    
    //私有化init方法并重写，保证单例的唯一性
        private override init(){
    //重写init，用来保证init方法的私有性，这样才能保证单例是真正唯一的，避免外部对象通过访问init方法创建单例类的其他实例。
    //这样做就可以保证编译器在某个类尝试使用()来初始化HLUniversalFunc时，抛出错误
    //eg: let tmpfunc = HLUniversalFunc()
    //会报错: 'HLUniversalFunc' initializer is inaccessible due to 'private' protection level
            super.init()
        }
}

extension SwiftFunc{
//MARK: - 类方法
/*类方法，即可以使用SwiftFunc.xxx来引用的方法,可以用class func来定义，也可以用static func来定义
但static func修饰的，继承时不能被重载，即static func相当于class final func
 */
    
//按比例缩放宽高坐标系
    static func scaleWidth(_ width:Double)->Double{
        return width * _SCALE_WIDTH_UNIT
    }
    static func scale_Height(_ height:Double)->Double{
        return height * _SCALE_HEIGHT_UNIT
    }
    
//取2个浮点数区间
    static func randomFloatBetween(_ smallNumber: CGFloat, and bigNumber: CGFloat) -> Float {
        let diff = bigNumber - smallNumber
        return Float((CGFloat(arc4random()) / 100.0).truncatingRemainder(dividingBy: diff) + smallNumber)
    }
     
//将当前屏幕转成Image
    static func captureScreen2Image() -> UIImage? {
//        let screen = UIScreen.main
//        let scale = screen.scale
//        let bounds = screen.bounds
//        let size = bounds.size
//        UIGraphicsBeginImageContextWithOptions(size, false, scale)
//        guard let _ = UIGraphicsGetCurrentContext() else {
//            return nil
//        }
//        let screenView = screen.snapshotView(afterScreenUpdates: false)
//        screenView.drawHierarchy(in: bounds, afterScreenUpdates: false) //无效
//        //screen.focusedView?.drawHierarchy(in: bounds, afterScreenUpdates: true) //也无效
//        let image = UIGraphicsGetImageFromCurrentImageContext()
//        UIGraphicsEndImageContext()
//        return image
//以上截屏无效，改用：
        let scene = UIApplication.shared.connectedScenes.first
        guard let windowScene = scene as? UIWindowScene else  {return nil}
        if let window = windowScene.windows.first{
            return window.snapshot2Image()
        }
        return nil
    }
    
//拷贝图像的指定区域
    static func copyImageFrom(Big image:UIImage,at rect:CGRect)->UIImage?{
        let newImageRef = image.cgImage?.cropping(to: rect) //CGImageCreateWithImageInRect
        if let imgref = newImageRef{
            let newImage = UIImage(cgImage: imgref)
            return newImage
        }else{
            return nil
        }
    }
    
//将字符串还原为类类型
    static func getClassFromString(_ clsName:String)->AnyClass?{
        let nameSpace = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String //eg：SwiftLearning
        guard let cls = NSClassFromString(nameSpace + "." + clsName) as? UIViewController.Type else {//eg:SwiftLearning.GradientLabelVC
            return nil
        }
        return cls
//用法eg：if let cls = SwiftFunc.getClassFromString(clsName) as? UIViewController.Type {...}
    }
    
//根据Font获取内容所占Size
    static func sizeWithText(_ text:String,font:UIFont,maxSize:CGSize)->CGSize{
        let atts = [NSAttributedString.Key.font:font]
        return text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: atts, context: nil).size
    }
    
    static func sizeWithText(_ text:String,font:UIFont)->CGSize{
        let maxSize = CGSizeMake(CGFloat(MAXFLOAT), font.lineHeight)
        return self.sizeWithText(text, font: font, maxSize: maxSize)
    }
    
}

extension SwiftFunc{
//MARK: - 成员方法
}
